package com.example.fw;

import java.util.Properties;
import java.util.logging.Logger;

import org.netbeans.jemmy.ClassReference;
import org.netbeans.jemmy.operators.JFrameOperator;

public class ApplicationManager {
	
	private static ApplicationManager singleton;		
	private FolderHelper folderHelper;
	private MenuHelper menuHelper;
	private Properties props;
	private JFrameOperator mainFrame;
	private Logger log = Logger.getLogger("Manager");
	
	public static ApplicationManager getInstance() {
		if (singleton == null) {
			singleton = new ApplicationManager();		
		}
		return singleton;
	}	
	
	public FolderHelper getFolderHelper() {
		if (folderHelper == null) {
			folderHelper = new FolderHelper(this);
		}
		return folderHelper;
	}
	
	public MenuHelper getMenuHelper() {
		if (menuHelper== null) {
			menuHelper = new MenuHelper(this);
		}
		return menuHelper;
	}
	
	public void setProperties(Properties props) {
		this.props = props;
	}
	
	public String getProperty(String key, String defaultValue){
		return props.getProperty(key, defaultValue);
	}

	public JFrameOperator getApplication(){
		if (mainFrame == null){
			log.info("Start application");
			try {
				new ClassReference("addressbook.AddressBookFrame").startApplication();				
			} catch (Exception e) {
				e.printStackTrace();				
			}
			mainFrame = new JFrameOperator("JAddressBook");
			log.info("Application started");
		}
		return mainFrame;
	}
	
	public void stop() {
		log.info("Close application");
		getApplication().requestClose();
		log.info("Application closed");
	}
}
